1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gio.ApplicationCommandLine;
26 
27 private import gio.FileIF;
28 private import gio.InputStream;
29 private import gio.c.functions;
30 public  import gio.c.types;
31 private import glib.Str;
32 private import glib.Variant;
33 private import glib.VariantDict;
34 private import glib.c.functions;
35 private import gobject.ObjectG;
36 
37 
38 /**
39  * #GApplicationCommandLine represents a command-line invocation of
40  * an application.  It is created by #GApplication and emitted
41  * in the #GApplication::command-line signal and virtual function.
42  * 
43  * The class contains the list of arguments that the program was invoked
44  * with.  It is also possible to query if the commandline invocation was
45  * local (ie: the current process is running in direct response to the
46  * invocation) or remote (ie: some other process forwarded the
47  * commandline to this process).
48  * 
49  * The GApplicationCommandLine object can provide the @argc and @argv
50  * parameters for use with the #GOptionContext command-line parsing API,
51  * with the g_application_command_line_get_arguments() function. See
52  * [gapplication-example-cmdline3.c][gapplication-example-cmdline3]
53  * for an example.
54  * 
55  * The exit status of the originally-invoked process may be set and
56  * messages can be printed to stdout or stderr of that process.  The
57  * lifecycle of the originally-invoked process is tied to the lifecycle
58  * of this object (ie: the process exits when the last reference is
59  * dropped).
60  * 
61  * The main use for #GApplicationCommandLine (and the
62  * #GApplication::command-line signal) is 'Emacs server' like use cases:
63  * You can set the `EDITOR` environment variable to have e.g. git use
64  * your favourite editor to edit commit messages, and if you already
65  * have an instance of the editor running, the editing will happen
66  * in the running instance, instead of opening a new one. An important
67  * aspect of this use case is that the process that gets started by git
68  * does not return until the editing is done.
69  * 
70  * Normally, the commandline is completely handled in the
71  * #GApplication::command-line handler. The launching instance exits
72  * once the signal handler in the primary instance has returned, and
73  * the return value of the signal handler becomes the exit status
74  * of the launching instance.
75  * |[<!-- language="C" -->
76  * static int
77  * command_line (GApplication            *application,
78  * GApplicationCommandLine *cmdline)
79  * {
80  * gchar **argv;
81  * gint argc;
82  * gint i;
83  * 
84  * argv = g_application_command_line_get_arguments (cmdline, &argc);
85  * 
86  * g_application_command_line_print (cmdline,
87  * "This text is written back\n"
88  * "to stdout of the caller\n");
89  * 
90  * for (i = 0; i < argc; i++)
91  * g_print ("argument %d: %s\n", i, argv[i]);
92  * 
93  * g_strfreev (argv);
94  * 
95  * return 0;
96  * }
97  * ]|
98  * The complete example can be found here:
99  * [gapplication-example-cmdline.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gapplication-example-cmdline.c)
100  * 
101  * In more complicated cases, the handling of the commandline can be
102  * split between the launcher and the primary instance.
103  * |[<!-- language="C" -->
104  * static gboolean
105  * test_local_cmdline (GApplication   *application,
106  * gchar        ***arguments,
107  * gint           *exit_status)
108  * {
109  * gint i, j;
110  * gchar **argv;
111  * 
112  * argv = *arguments;
113  * 
114  * if (argv[0] == NULL)
115  * {
116  * *exit_status = 0;
117  * return FALSE;
118  * }
119  * 
120  * i = 1;
121  * while (argv[i])
122  * {
123  * if (g_str_has_prefix (argv[i], "--local-"))
124  * {
125  * g_print ("handling argument %s locally\n", argv[i]);
126  * g_free (argv[i]);
127  * for (j = i; argv[j]; j++)
128  * argv[j] = argv[j + 1];
129  * }
130  * else
131  * {
132  * g_print ("not handling argument %s locally\n", argv[i]);
133  * i++;
134  * }
135  * }
136  * 
137  * *exit_status = 0;
138  * 
139  * return FALSE;
140  * }
141  * 
142  * static void
143  * test_application_class_init (TestApplicationClass *class)
144  * {
145  * G_APPLICATION_CLASS (class)->local_command_line = test_local_cmdline;
146  * 
147  * ...
148  * }
149  * ]|
150  * In this example of split commandline handling, options that start
151  * with `--local-` are handled locally, all other options are passed
152  * to the #GApplication::command-line handler which runs in the primary
153  * instance.
154  * 
155  * The complete example can be found here:
156  * [gapplication-example-cmdline2.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gapplication-example-cmdline2.c)
157  * 
158  * If handling the commandline requires a lot of work, it may
159  * be better to defer it.
160  * |[<!-- language="C" -->
161  * static gboolean
162  * my_cmdline_handler (gpointer data)
163  * {
164  * GApplicationCommandLine *cmdline = data;
165  * 
166  * // do the heavy lifting in an idle
167  * 
168  * g_application_command_line_set_exit_status (cmdline, 0);
169  * g_object_unref (cmdline); // this releases the application
170  * 
171  * return G_SOURCE_REMOVE;
172  * }
173  * 
174  * static int
175  * command_line (GApplication            *application,
176  * GApplicationCommandLine *cmdline)
177  * {
178  * // keep the application running until we are done with this commandline
179  * g_application_hold (application);
180  * 
181  * g_object_set_data_full (G_OBJECT (cmdline),
182  * "application", application,
183  * (GDestroyNotify)g_application_release);
184  * 
185  * g_object_ref (cmdline);
186  * g_idle_add (my_cmdline_handler, cmdline);
187  * 
188  * return 0;
189  * }
190  * ]|
191  * In this example the commandline is not completely handled before
192  * the #GApplication::command-line handler returns. Instead, we keep
193  * a reference to the #GApplicationCommandLine object and handle it
194  * later (in this example, in an idle). Note that it is necessary to
195  * hold the application until you are done with the commandline.
196  * 
197  * The complete example can be found here:
198  * [gapplication-example-cmdline3.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gapplication-example-cmdline3.c)
199  */
200 public class ApplicationCommandLine : ObjectG
201 {
202 	/** the main Gtk struct */
203 	protected GApplicationCommandLine* gApplicationCommandLine;
204 
205 	/** Get the main Gtk struct */
206 	public GApplicationCommandLine* getApplicationCommandLineStruct(bool transferOwnership = false)
207 	{
208 		if (transferOwnership)
209 			ownedRef = false;
210 		return gApplicationCommandLine;
211 	}
212 
213 	/** the main Gtk struct as a void* */
214 	protected override void* getStruct()
215 	{
216 		return cast(void*)gApplicationCommandLine;
217 	}
218 
219 	/**
220 	 * Sets our main struct and passes it to the parent class.
221 	 */
222 	public this (GApplicationCommandLine* gApplicationCommandLine, bool ownedRef = false)
223 	{
224 		this.gApplicationCommandLine = gApplicationCommandLine;
225 		super(cast(GObject*)gApplicationCommandLine, ownedRef);
226 	}
227 
228 
229 	/** */
230 	public static GType getType()
231 	{
232 		return g_application_command_line_get_type();
233 	}
234 
235 	/**
236 	 * Creates a #GFile corresponding to a filename that was given as part
237 	 * of the invocation of @cmdline.
238 	 *
239 	 * This differs from g_file_new_for_commandline_arg() in that it
240 	 * resolves relative pathnames using the current working directory of
241 	 * the invoking process rather than the local process.
242 	 *
243 	 * Params:
244 	 *     arg = an argument from @cmdline
245 	 *
246 	 * Returns: a new #GFile
247 	 *
248 	 * Since: 2.36
249 	 */
250 	public FileIF createFileForArg(string arg)
251 	{
252 		auto __p = g_application_command_line_create_file_for_arg(gApplicationCommandLine, Str.toStringz(arg));
253 
254 		if(__p is null)
255 		{
256 			return null;
257 		}
258 
259 		return ObjectG.getDObject!(FileIF)(cast(GFile*) __p, true);
260 	}
261 
262 	/**
263 	 * Gets the list of arguments that was passed on the command line.
264 	 *
265 	 * The strings in the array may contain non-UTF-8 data on UNIX (such as
266 	 * filenames or arguments given in the system locale) but are always in
267 	 * UTF-8 on Windows.
268 	 *
269 	 * If you wish to use the return value with #GOptionContext, you must
270 	 * use g_option_context_parse_strv().
271 	 *
272 	 * The return value is %NULL-terminated and should be freed using
273 	 * g_strfreev().
274 	 *
275 	 * Returns: the string array containing the arguments (the argv)
276 	 *
277 	 * Since: 2.28
278 	 */
279 	public string[] getArguments()
280 	{
281 		int argc;
282 
283 		auto retStr = g_application_command_line_get_arguments(gApplicationCommandLine, &argc);
284 
285 		scope(exit) Str.freeStringArray(retStr);
286 		return Str.toStringArray(retStr, argc);
287 	}
288 
289 	/**
290 	 * Gets the working directory of the command line invocation.
291 	 * The string may contain non-utf8 data.
292 	 *
293 	 * It is possible that the remote application did not send a working
294 	 * directory, so this may be %NULL.
295 	 *
296 	 * The return value should not be modified or freed and is valid for as
297 	 * long as @cmdline exists.
298 	 *
299 	 * Returns: the current directory, or %NULL
300 	 *
301 	 * Since: 2.28
302 	 */
303 	public string getCwd()
304 	{
305 		return Str.toString(g_application_command_line_get_cwd(gApplicationCommandLine));
306 	}
307 
308 	/**
309 	 * Gets the contents of the 'environ' variable of the command line
310 	 * invocation, as would be returned by g_get_environ(), ie as a
311 	 * %NULL-terminated list of strings in the form 'NAME=VALUE'.
312 	 * The strings may contain non-utf8 data.
313 	 *
314 	 * The remote application usually does not send an environment.  Use
315 	 * %G_APPLICATION_SEND_ENVIRONMENT to affect that.  Even with this flag
316 	 * set it is possible that the environment is still not available (due
317 	 * to invocation messages from other applications).
318 	 *
319 	 * The return value should not be modified or freed and is valid for as
320 	 * long as @cmdline exists.
321 	 *
322 	 * See g_application_command_line_getenv() if you are only interested
323 	 * in the value of a single environment variable.
324 	 *
325 	 * Returns: the environment strings, or %NULL if they were not sent
326 	 *
327 	 * Since: 2.28
328 	 */
329 	public string[] getEnviron()
330 	{
331 		return Str.toStringArray(g_application_command_line_get_environ(gApplicationCommandLine));
332 	}
333 
334 	/**
335 	 * Gets the exit status of @cmdline.  See
336 	 * g_application_command_line_set_exit_status() for more information.
337 	 *
338 	 * Returns: the exit status
339 	 *
340 	 * Since: 2.28
341 	 */
342 	public int getExitStatus()
343 	{
344 		return g_application_command_line_get_exit_status(gApplicationCommandLine);
345 	}
346 
347 	/**
348 	 * Determines if @cmdline represents a remote invocation.
349 	 *
350 	 * Returns: %TRUE if the invocation was remote
351 	 *
352 	 * Since: 2.28
353 	 */
354 	public bool getIsRemote()
355 	{
356 		return g_application_command_line_get_is_remote(gApplicationCommandLine) != 0;
357 	}
358 
359 	/**
360 	 * Gets the options there were passed to g_application_command_line().
361 	 *
362 	 * If you did not override local_command_line() then these are the same
363 	 * options that were parsed according to the #GOptionEntrys added to the
364 	 * application with g_application_add_main_option_entries() and possibly
365 	 * modified from your GApplication::handle-local-options handler.
366 	 *
367 	 * If no options were sent then an empty dictionary is returned so that
368 	 * you don't need to check for %NULL.
369 	 *
370 	 * Returns: a #GVariantDict with the options
371 	 *
372 	 * Since: 2.40
373 	 */
374 	public VariantDict getOptionsDict()
375 	{
376 		auto __p = g_application_command_line_get_options_dict(gApplicationCommandLine);
377 
378 		if(__p is null)
379 		{
380 			return null;
381 		}
382 
383 		return new VariantDict(cast(GVariantDict*) __p);
384 	}
385 
386 	/**
387 	 * Gets the platform data associated with the invocation of @cmdline.
388 	 *
389 	 * This is a #GVariant dictionary containing information about the
390 	 * context in which the invocation occurred.  It typically contains
391 	 * information like the current working directory and the startup
392 	 * notification ID.
393 	 *
394 	 * For local invocation, it will be %NULL.
395 	 *
396 	 * Returns: the platform data, or %NULL
397 	 *
398 	 * Since: 2.28
399 	 */
400 	public Variant getPlatformData()
401 	{
402 		auto __p = g_application_command_line_get_platform_data(gApplicationCommandLine);
403 
404 		if(__p is null)
405 		{
406 			return null;
407 		}
408 
409 		return new Variant(cast(GVariant*) __p, true);
410 	}
411 
412 	/**
413 	 * Gets the stdin of the invoking process.
414 	 *
415 	 * The #GInputStream can be used to read data passed to the standard
416 	 * input of the invoking process.
417 	 * This doesn't work on all platforms.  Presently, it is only available
418 	 * on UNIX when using a D-Bus daemon capable of passing file descriptors.
419 	 * If stdin is not available then %NULL will be returned.  In the
420 	 * future, support may be expanded to other platforms.
421 	 *
422 	 * You must only call this function once per commandline invocation.
423 	 *
424 	 * Returns: a #GInputStream for stdin
425 	 *
426 	 * Since: 2.34
427 	 */
428 	public InputStream getStdin()
429 	{
430 		auto __p = g_application_command_line_get_stdin(gApplicationCommandLine);
431 
432 		if(__p is null)
433 		{
434 			return null;
435 		}
436 
437 		return ObjectG.getDObject!(InputStream)(cast(GInputStream*) __p, true);
438 	}
439 
440 	/**
441 	 * Gets the value of a particular environment variable of the command
442 	 * line invocation, as would be returned by g_getenv().  The strings may
443 	 * contain non-utf8 data.
444 	 *
445 	 * The remote application usually does not send an environment.  Use
446 	 * %G_APPLICATION_SEND_ENVIRONMENT to affect that.  Even with this flag
447 	 * set it is possible that the environment is still not available (due
448 	 * to invocation messages from other applications).
449 	 *
450 	 * The return value should not be modified or freed and is valid for as
451 	 * long as @cmdline exists.
452 	 *
453 	 * Params:
454 	 *     name = the environment variable to get
455 	 *
456 	 * Returns: the value of the variable, or %NULL if unset or unsent
457 	 *
458 	 * Since: 2.28
459 	 */
460 	public string getenv(string name)
461 	{
462 		return Str.toString(g_application_command_line_getenv(gApplicationCommandLine, Str.toStringz(name)));
463 	}
464 
465 	/**
466 	 * Sets the exit status that will be used when the invoking process
467 	 * exits.
468 	 *
469 	 * The return value of the #GApplication::command-line signal is
470 	 * passed to this function when the handler returns.  This is the usual
471 	 * way of setting the exit status.
472 	 *
473 	 * In the event that you want the remote invocation to continue running
474 	 * and want to decide on the exit status in the future, you can use this
475 	 * call.  For the case of a remote invocation, the remote process will
476 	 * typically exit when the last reference is dropped on @cmdline.  The
477 	 * exit status of the remote process will be equal to the last value
478 	 * that was set with this function.
479 	 *
480 	 * In the case that the commandline invocation is local, the situation
481 	 * is slightly more complicated.  If the commandline invocation results
482 	 * in the mainloop running (ie: because the use-count of the application
483 	 * increased to a non-zero value) then the application is considered to
484 	 * have been 'successful' in a certain sense, and the exit status is
485 	 * always zero.  If the application use count is zero, though, the exit
486 	 * status of the local #GApplicationCommandLine is used.
487 	 *
488 	 * Params:
489 	 *     exitStatus = the exit status
490 	 *
491 	 * Since: 2.28
492 	 */
493 	public void setExitStatus(int exitStatus)
494 	{
495 		g_application_command_line_set_exit_status(gApplicationCommandLine, exitStatus);
496 	}
497 }